<?php

/**
 *  +----------------------------------------------------------------------
 *  | 文件功能：SystemController.php：
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2022~2025 https://www.huihuiv.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Create At: 2025年05月16日 20:57:16
 *  +----------------------------------------------------------------------
 *  | Author: 半壶水 <star@cqshanwen.com>
 *  +----------------------------------------------------------------------
 */

namespace App\Http\Controllers\Api;

use App\Utils\Message;
use Brotzka\DotenvEditor\DotenvEditor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class SystemController extends BaseController
{
    /**
     * 修改系统配置——环境变量
     *
     * @param Request $request
     * @param DotenvEditor $dotenvEditor
     * @return \Illuminate\Http\JsonResponse
     */
    public function set_env_info(Request $request, DotenvEditor $dotenvEditor)
    {
        // 按 .env 中的实际键名构造大写数组
        $settings = [
            // 支付宝支付
            'ALIPAY_ID'           => $request->alipay_id,
            'ALIPAY_KEY'          => $request->alipay_key,
            'ALIPAY_SIGN_TYPE'    => $request->alipay_sign_type,
            'ALIPAY_CHARSET'      => $request->alipay_charset,
            'ALIPAY_TRANSPORT'    => $request->alipay_transport,
            'ALIPAY_APIURL'       => $request->alipay_apiurl,
            'ALIPAY_NOTIFY_URL'   => $request->alipay_notify_url,
            'ALIPAY_RETURN_URL'   => $request->alipay_return_url,

            // 充值
            'TOPUP_ID'            => $request->topup_id,
            'TOPUP_APPKEY'        => $request->topup_appkey,
            'TOPUP_API'           => $request->topup_api,
            'TOPUP_PRODUCT_ID'    => $request->topup_product_id,
            'TOPUP_NUMBER'        => $request->topup_number,
            'TOPUP_AMOUNT'        => $request->topup_amount,
            'TOPUP_NOTIFY_URL'    => $request->topup_notify_url,

            // 客服链接和提示文本
            'SERVICE_LINK'        => $request->service_link,
            'TIP_TEXT'            => $request->tip_text,
        ];

        // 更新 .env 中已存在的键（区分大小写）
        $dotenvEditor->changeEnv($settings);

        // 清除并重建配置缓存
        Artisan::call('config:clear');
        Artisan::call('config:cache');

        return Message::show_message(true, '系统设置修改成功');
    }

    /**
     * 后台专用——获取所有环境配置
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function manager_get_env_info()
    {
        $setting = [
            // 支付宝支付
            'alipay_id'           => env('ALIPAY_ID', ''),
            'alipay_key'          => env('ALIPAY_KEY', ''),
            'alipay_sign_type'    => env('ALIPAY_SIGN_TYPE', ''),
            'alipay_charset'      => env('ALIPAY_CHARSET', ''),
            'alipay_transport'    => env('ALIPAY_TRANSPORT', ''),
            'alipay_apiurl'       => env('ALIPAY_APIURL', ''),
            'alipay_notify_url'   => env('ALIPAY_NOTIFY_URL', ''),
            'alipay_return_url'   => env('ALIPAY_RETURN_URL', ''),

            // 充值
            'topup_id'            => env('TOPUP_ID', ''),
            'topup_appkey'        => env('TOPUP_APPKEY', ''),
            'topup_api'           => env('TOPUP_API', ''),
            'topup_product_id'    => env('TOPUP_PRODUCT_ID', ''),
            'topup_number'        => env('TOPUP_NUMBER', ''),
            'topup_amount'        => env('TOPUP_AMOUNT', ''),
            'topup_notify_url'    => env('TOPUP_NOTIFY_URL', ''),

            // 客服链接和提示文本
            'service_link'        => env('SERVICE_LINK', ''),
            'tip_text'            => env('TIP_TEXT', ''),
        ];

        return Message::show_message(true, 'success', $setting);
    }

    /**
     * 对外提供轻量版——获取部分环境配置
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function get_env_info()
    {
        $setting = [
            'topup_number'  => env('TOPUP_NUMBER', ''),
            'topup_amount'  => env('TOPUP_AMOUNT', ''),
            'service_link'  => env('SERVICE_LINK', ''),
            'tip_text'      => env('TIP_TEXT', ''),
        ];

        return Message::show_message(true, 'success', $setting);
    }
}
